package com.ruoyi.web.controller.pushSetting;

import java.util.Arrays;
import java.util.List;

import com.ruoyi.circle.task.CirclePushTask;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.pushSetting.domain.CircleHsPushSetting;
import com.ruoyi.pushSetting.service.ICircleHsPushSettingService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;

/**
 * 圈子热搜推送设置Controller
 * 
 * @author 区嘉盛
 * @date 2025-06-23
 */
@RestController
@RequestMapping("/pushSetting/pushSetting")
@Tag(name = "【圈子热搜推送设置】管理")
public class CircleHsPushSettingController extends BaseController
{
    @Autowired
    private ICircleHsPushSettingService circleHsPushSettingService;

    @Autowired
    private CirclePushTask circlePushTask;

    /**
     * 查询圈子热搜推送设置列表
     */
    @Operation(summary = "查询圈子热搜推送设置列表")
    @PreAuthorize("@ss.hasPermi('pushSetting:pushSetting:list')")
    @GetMapping("/list")
    public TableDataInfo list(CircleHsPushSetting circleHsPushSetting)
    {
        startPage();
        List<CircleHsPushSetting> list = circleHsPushSettingService.selectCircleHsPushSettingList(circleHsPushSetting);
        return getDataTable(list);
    }

    /**
     * 导出圈子热搜推送设置列表
     */
    @Operation(summary = "导出圈子热搜推送设置列表")
    @PreAuthorize("@ss.hasPermi('pushSetting:pushSetting:export')")
    @Log(title = "圈子热搜推送设置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CircleHsPushSetting circleHsPushSetting)
    {
        List<CircleHsPushSetting> list = circleHsPushSettingService.selectCircleHsPushSettingList(circleHsPushSetting);
        ExcelUtil<CircleHsPushSetting> util = new ExcelUtil<CircleHsPushSetting>(CircleHsPushSetting.class);
        util.exportExcel(response, list, "圈子热搜推送设置数据");
    }

    /**
     * 获取圈子热搜推送设置详细信息
     */
    @Operation(summary = "获取圈子热搜推送设置详细信息")
    @PreAuthorize("@ss.hasPermi('pushSetting:pushSetting:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(circleHsPushSettingService.selectCircleHsPushSettingById(id));
    }

    /**
     * 新增圈子热搜推送设置
     */
    @Operation(summary = "新增圈子热搜推送设置")
    @PreAuthorize("@ss.hasPermi('pushSetting:pushSetting:add')")
    @Log(title = "圈子热搜推送设置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CircleHsPushSetting circleHsPushSetting)
    {
        circleHsPushSetting.setCreatedBy(getUsername());
        return toAjax(circleHsPushSettingService.insertCircleHsPushSetting(circleHsPushSetting));
    }

    /**
     * 修改圈子热搜推送设置
     */
    @Operation(summary = "修改圈子热搜推送设置")
    @PreAuthorize("@ss.hasPermi('pushSetting:pushSetting:edit')")
    @Log(title = "圈子热搜推送设置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CircleHsPushSetting circleHsPushSetting)
    {
        circleHsPushSetting.setUpdatedBy(getUsername());
        return toAjax(circleHsPushSettingService.updateCircleHsPushSetting(circleHsPushSetting));
    }

    /**
     * 删除圈子热搜推送设置
     */
    @Operation(summary = "删除圈子热搜推送设置")
    @PreAuthorize("@ss.hasPermi('pushSetting:pushSetting:remove')")
    @Log(title = "圈子热搜推送设置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable( name = "ids" ) Long[] ids) 
    {
        return toAjax(circleHsPushSettingService.deleteCircleHsPushSettingByIds(ids));
    }

    /**
     * 立即推送热搜内容到指定圈子
     */
    @Operation(summary = "立即推送热搜内容到指定圈子")
    @PreAuthorize("@ss.hasPermi('pushSetting:pushSetting:edit')")
    @Log(title = "圈子热搜推送设置", businessType = BusinessType.UPDATE)
    @PostMapping("/pushNow/{id}")
    public AjaxResult pushNow(@PathVariable("id") Long id)
    {
        try {
            // 获取推送设置信息
            CircleHsPushSetting pushSetting = circleHsPushSettingService.selectCircleHsPushSettingById(id);
            if (pushSetting == null) {
                return error("推送设置不存在");
            }

            // 检查推送功能是否开启
            if (pushSetting.getPushStatus() == null || pushSetting.getPushStatus() != 1L) {
                return error("该圈子的推送功能未开启");
            }

            // 立即执行推送任务
            List<Long> circleIds = Arrays.asList(pushSetting.getCircleId());
            circlePushTask.batchPushHotSearchToCircles(circleIds);

            return success("推送任务执行成功");
        } catch (Exception e) {
            logger.error("立即推送失败", e);
            return error("推送任务执行失败：" + e.getMessage());
        }
    }
}
